from pyecharts.faker import Faker
import numpy as np
from pyecharts import options as opts
from pyecharts.faker import Faker
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Pie

# data1 = [
#     list(z)
#     for z in zip(
#         Faker.choose() + Faker.choose() + Faker.choose() + Faker.choose() + Faker.choose(),
#         Faker.values() + Faker.values() + Faker.values() + Faker.values() + Faker.values(),
#         )
# ]
# print(data1)
# bing = [
#     list(z)
#     for z in zip(
#         Faker.choose() + Faker.choose() + Faker.choose() + Faker.choose() + Faker.choose(),
#         Faker.values() + Faker.values() + Faker.values() + Faker.values() + Faker.values(),
#         )
# ]
data1 = []
def test():
    global data1
    import pandas as pd
    from sqlalchemy import create_engine
    engine = create_engine('mysql+pymysql://root:123456@localhost:3306/pythondata')
    # 查询语句，选出employee表中的所有数据
    sql = ''' select  city,count(*) as count from test group by city ORDER BY count ASC;'''
    # read_sql_query的两个参数: sql语句， 数据库连接
    city = pd.read_sql_query(sql, engine)
    data1 = np.array(city)  # 先将数据框转换为数组
    data1 = data1.tolist()  # 其次转换为列表
    print(data1)  # 以数组形式打出来方便看

def pie_chart() -> Pie:
    # ********* Begin *********#
    global data1
    pie = (
        Pie()
    .add("",data1,center=["40%", "50%"])
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Pie-Legend 滚动"),
        legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical"),
    )
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    )
    # ********** End **********#
    return pie
test()
# make_snapshot(snapshot, pie_chart().render("饼图.html"), "student_answer.png") # 输出图片
